//
//  MainNavSearchView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/8.
//

import UIKit
import Alamofire
class MainNavSearchView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    private let searchImage = UIImageView()
    private  let textView = UIView()
    private let hotlabel = UILabel()
    private let hotlabel2 = UILabel()
    private var hotKeys:Array<String> = []
    private var index = 0
    private let maskLayer = CAGradientLayer()
    private let maskv = UIView()
    
    init() {
        super.init(frame: CGRect.zero)
        SystemLog("MainNavSearchView - init")
        let sh = UIApplication.shared.statusBarFrame.height
        backgroundColor = RED_COLOR
      
        let image = UIImage(named: "push")?.reSizeImage(reSize: CGSize.init(width: 20, height: 20))
        let sendView = UIView()
        addSubview(sendView)
        sendView.snp.makeConstraints({
            $0.right.equalToSuperview().inset(5)
            $0.width.equalTo(40)
            $0.centerY.equalToSuperview().offset(sh/2)
        })
        let pv = UIImageView()
        pv.image = image
        sendView.addSubview(pv)
        pv.snp.makeConstraints({
            $0.top.equalTo(0)
            $0.centerX.equalToSuperview()
        })
        let pl = UILabel()
        pl.text = "发布"
        pl.textColor = .white
        sendView.addSubview(pl)
        pl.font = Font_Size_10
        pl.snp.makeConstraints({
            $0.top.equalTo(pv.snp.bottom)
            $0.bottom.equalToSuperview()
            $0.centerX.equalTo(pv)
        })
        
        let tgr = UITapGestureRecognizer.init(target: self, action: #selector(pushTap))
        sendView.addGestureRecognizer(tgr)
        
        let navView = UIView()
        navView.backgroundColor = .white
        navView.layer.cornerRadius = 18
        navView.clipsToBounds = true
        addSubview(navView)
        navView.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.right.equalTo(sendView.snp.left)
            $0.height.equalTo(35)
            $0.centerY.equalToSuperview().offset(sh/2)
        })
        
        let searchIcon = UIImage(named: "search")?.reSizeImage(reSize: CGSize.init(width: 15, height: 15)).withRenderingMode(.alwaysTemplate)
        searchImage.image = searchIcon
        searchImage.tintColor = B000_COLOR
        navView.addSubview(searchImage)
        searchImage.snp.makeConstraints({
            $0.left.equalTo(10)
            $0.size.equalTo(searchIcon!.size)
            $0.centerY.equalToSuperview()
        })
        
       
        navView.addSubview(textView)
        textView.snp.makeConstraints({
            $0.left.equalTo(searchImage.snp.right).offset(5)
            $0.top.equalTo(0)
            $0.height.equalToSuperview().multipliedBy(2)
            $0.right.equalToSuperview().inset(10)
        })
        
        
        hotKeys = ["全球日增确诊超65万例 | 搜捕犬追捕金钱豹累坏被注入拎上山","谈谈创始人离职 | 谈谈创始人离职","印度制氧机订单激增 | 印度制氧机订单激增","更多热点资讯尽在天天头条。。。。。。"]
        AF.request("https://tsearch.snssdk.com/search/suggest/hot_words/",method: .get).response{ [self]
            res in
            let json = res.data?.json()
            for item in json!["data"].array!{
                hotKeys.append("\(item["query"])")
            }
            self.hotlabel.text = self.hotKeys.first
            self.hotlabel2.text = self.hotKeys[self.index + 1]
        }
        hotlabel.font = Font_Size_14
        hotlabel.text = hotKeys.first
        hotlabel.lineBreakMode = .byClipping
        hotlabel.textColor = B000_COLOR
        textView.addSubview(hotlabel)
        hotlabel.snp.makeConstraints({
            $0.left.top.right.equalTo(0)
            $0.height.equalToSuperview().dividedBy(2)
        })
        hotlabel2.font = Font_Size_14
        hotlabel2.text = hotKeys[index + 1]
        hotlabel2.lineBreakMode = .byClipping
        hotlabel2.textColor = B000_COLOR
        textView.addSubview(hotlabel2)
        hotlabel2.snp.makeConstraints({
            $0.left.right.equalTo(0)
            $0.height.equalToSuperview().dividedBy(2)
            $0.top.equalTo(hotlabel.snp.bottom)
        })
        
        

        let searchTap = UITapGestureRecognizer.init(target: self, action: #selector(search))
        navView.addGestureRecognizer(searchTap)
        index += 1
        self.perform(#selector(startAnimation), with: self, afterDelay: 10)
        
        navView.addSubview(maskv)
        maskv.snp.makeConstraints({
            $0.height.equalToSuperview()
            $0.right.equalTo(0)
            $0.width.equalTo(30)
        })
        maskLayer.startPoint = CGPoint.init(x: 0, y: 0 )
        maskLayer.endPoint = CGPoint.init(x: 1, y: 0)
        maskLayer.colors = [UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5).cgColor,UIColor.white.cgColor]
        maskv.layer.addSublayer(maskLayer)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        maskLayer.frame = CGRect.init(x: 0, y: 0, width: 30, height: self.fatherViewController()!.navigationBarHeight)
    }
   
    @objc func startAnimation(){
        textView.snp.remakeConstraints({
            $0.left.equalTo(searchImage.snp.right).offset(5)
            $0.bottom.equalTo(textView.superview!.snp.bottom)
            $0.right.equalToSuperview().inset(10)
            $0.height.equalToSuperview().multipliedBy(2)
        })
        UIView.animate(withDuration: 0.5) {[weak self] in
            self?.layoutIfNeeded()
        }completion: { [weak self](res) in
            self?.hotlabel.text = self?.hotlabel2.text
            self?.textView.snp.remakeConstraints({
                $0.left.equalTo((self?.searchImage.snp.right)!).offset(5)
                $0.top.equalTo(0)
                $0.height.equalToSuperview().multipliedBy(2)
                $0.right.equalToSuperview().inset(10)
            })
            self?.layoutIfNeeded()
            let index = self?.index
            if index! + 1 >= (self?.hotKeys.count)!{
                self?.index = 0
            }else{
                self?.index += 1
            }
            self?.hotlabel2.text = self?.hotKeys[(self?.index)!]
            self?.perform(#selector(self?.startAnimation), with: self, afterDelay: 10)
        }
    }
    @objc func search(){
        let vc = MainSearchController()
        vc.title = hotlabel.text
        vc.modalPresentationStyle = .overCurrentContext
        vc.modalTransitionStyle = .crossDissolve
        let fvc = self.fatherViewController() as! CommonViewController
        fvc.pushViewController(vc, animated: false,hidesBottomBar: true)
    }
    @objc func pushTap(){
        let nav = UINavigationController.init(rootViewController: PublishMenuViewController())
        nav.modalPresentationStyle = .overCurrentContext
        nav.modalTransitionStyle = .crossDissolve
        nav.modalPresentationCapturesStatusBarAppearance = true
        self.fatherViewController()?.tabBarController?.present(nav, animated: false, completion: {

        })
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
